package chances.webui.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import chances.webui.annotation.Widget;

@SuppressWarnings("serial")
@Widget(name = "table")
public class Table implements java.io.Serializable, Component {
	private String id;
	private String searchUrl;
	private String dataUrl;
	private String formUrl;

	private int size;
	private boolean editable;
	private boolean showToolbar;
	private boolean showSearch;
	private boolean showExport;
	private boolean showRefresh;
	private boolean showToggle;
	private String styleClass;
	private String datas;
	private String title;
	
	private boolean append;
	private String postUrl;

	private String dataType;

	private List<Column> columns = new ArrayList<Column>();
	private Map<String, Object> lookups = new LinkedHashMap<String, Object>();

	public void addColumn(Column column) {
		this.columns.add(column);
	}

	public void addLookup(String name, Object lookup) {
		this.lookups.put(name, lookup);
	}

	public Set<Entry<String, String>> getLookupNames() {
		Map<String, String> value = new HashMap<String, String>();
		this.columns.stream().filter(s -> Objects.nonNull(s.getLookup())).forEach(s -> {
			value.put(s.getField(), s.getField() + "Lookup");
		});
		return value.entrySet();
	}

	public Column getSearchColumn() {
		Optional<Column> result = this.columns.stream().filter(s -> {
			return s.isSearchable();
		}).findFirst();
		if(result.isPresent()){
			return result.get();
		}else{
			return null;
		}
	}

	public Column getKeyColumn() {
		List<Column> keys = this.columns.stream().filter(s -> {
			return s.isKeyFiled();
		}).collect(Collectors.toList());
		if (!keys.isEmpty()) {
			return keys.get(0);
		} else {
			return null;
		}

	}

	public boolean isLookup() {
		return !lookups.isEmpty();
	}

	public List<Column> getColumns() {
		return columns;
	}


	public String getId() {
		return id;
	}

	public String getSearchUrl() {
		return searchUrl;
	}

	public String getDataUrl() {
		return dataUrl;
	}

	public int getSize() {
		return size;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setSearchUrl(String searchUrl) {
		this.searchUrl = searchUrl;
	}

	public void setDataUrl(String dataUrl) {
		this.dataUrl = dataUrl;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public boolean isShowToolbar() {
		return showToolbar;
	}

	public boolean isShowSearch() {
		return showSearch;
	}

	public boolean isShowExport() {
		return showExport;
	}

	public boolean isShowRefresh() {
		return showRefresh;
	}

	public boolean isShowToggle() {
		return showToggle;
	}

	public void setShowToolbar(boolean showToolbar) {
		this.showToolbar = showToolbar;
	}

	public void setShowSearch(boolean showSearch) {
		this.showSearch = showSearch;
	}

	public void setShowExport(boolean showExport) {
		this.showExport = showExport;
	}

	public void setShowRefresh(boolean showRefresh) {
		this.showRefresh = showRefresh;
	}

	public void setShowToggle(boolean showToggle) {
		this.showToggle = showToggle;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getFormUrl() {
		return formUrl;
	}



	public boolean isAppend() {
		return append;
	}

	public String getDataType() {
		return dataType;
	}

	public void setFormUrl(String formUrl) {
		this.formUrl = formUrl;
	}

	public Map<String, Object> getLookups() {
		return lookups;
	}


	public void setAppend(boolean append) {
		this.append = append;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public String getDatas() {
		return datas;
	}

	public void setDatas(String datas) {
		this.datas = datas;
	}

	public String getPostUrl() {
		return postUrl;
	}

	public void setPostUrl(String postUrl) {
		this.postUrl = postUrl;
	}
}
